package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("MaterialDAO")
@Transactional
public class MaterialDAOImpl extends AbstractJpaDao<Material> implements MaterialDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Material.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public MaterialDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Material findMaterialById(Integer id) throws DataAccessException {

		return findMaterialById(id, -1, -1);
	}

	@Transactional
	public Material findMaterialById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMaterialById", startResult, maxRows, id);
			return (Material) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Material> findMaterialByName(String name) throws DataAccessException {

		return findMaterialByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Material> findMaterialByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMaterialByName", startResult, maxRows, name);
		return new LinkedHashSet<Material>(query.getResultList());
	}

	@Transactional
	public Set<Material> findMaterialByNameContaining(String name) throws DataAccessException {

		return findMaterialByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Material> findMaterialByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMaterialByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Material>(query.getResultList());
	}

	@Transactional
	public Set<Material> findMaterialByQuality(String quality) throws DataAccessException {

		return findMaterialByQuality(quality, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Material> findMaterialByQuality(String quality, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMaterialByQuality", startResult, maxRows, quality);
		return new LinkedHashSet<Material>(query.getResultList());
	}

	@Transactional
	public Set<Material> findMaterialByQualityContaining(String quality) throws DataAccessException {

		return findMaterialByQualityContaining(quality, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Material> findMaterialByQualityContaining(String quality, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMaterialByQualityContaining", startResult, maxRows, quality);
		return new LinkedHashSet<Material>(query.getResultList());
	}

	@Transactional
	public Set<Material> findMaterialByDescription(String description) throws DataAccessException {

		return findMaterialByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Material> findMaterialByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMaterialByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Material>(query.getResultList());
	}

	@Transactional
	public Set<Material> findMaterialByDescriptionContaining(String description) throws DataAccessException {

		return findMaterialByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Material> findMaterialByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findMaterialByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Material>(query.getResultList());
	}

	@Transactional
	public Material findMaterialByPrimaryKey(Integer id) throws DataAccessException {
		return findMaterialByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Material findMaterialByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMaterialByPrimaryKey", startResult, maxRows, id);
			return (Material) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Material> findMaterialsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMaterialsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Material> findAllMaterials() throws DataAccessException {

		return findAllMaterials(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Material> findAllMaterials(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllMaterials", startResult, maxRows);
		return new LinkedHashSet<Material>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Material> findMaterialByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Material>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Material> findMaterialsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Material.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Material> materials = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return materials;
	}

	@Transactional
	public Material findMaterialByPhysicalPrimaryKey(String name, String quality) throws DataAccessException {
		return findMaterialByPhysicalPrimaryKey(name, quality, -1, -1);
	}

	@Transactional
	public Material findMaterialByPhysicalPrimaryKey(String name, String quality, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findMaterialByPhyscialPrimaryKey", startResult, maxRows, name, quality);
			return (Material) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Material entity) {
		return true;
	}
}
